<template>
  <div class="allBusiness">
    <div class="header flexbetween">
      <div class="flexleft" style="flex:1">
        <div class="shutup pointer" @click="shutup">
          <!-- 关闭 -->
          <i class="el-icon-close" style="color: #fff;font-size: 24px;"></i>
        </div>
        <div class="margin-left1 margin-right3">
          <img alt="" class="logoimg" src="@/assets/img/layout/logo.png">
        </div>
        <div class="title">全部企业</div>
      </div>
    </div>

    <div class="content">
      <div class="padding15top" style="width:1180px;margin:0 auto;">
        <el-row>
          <el-col :span="8" class="flexleft">
            <el-input v-model="input" placeholder="请输入企业名称"></el-input>
            <el-button type="primary" @click="searchCompany">搜索</el-button>
          </el-col>
        </el-row>
        <el-row class="flexbetween padding15top" style="align-items:flex-start;">
          <el-col :span="18" class="flexleft" style="flex-wrap:wrap;">
            <div class="companyList createCompany flexcolumn" @click="dialogFormVisible=true">
              <i class="el-icon-plus"></i>
              <p>创建/加入企业</p>
            </div>
            <div v-for="v in allCompany" :key="v.id" class="companyList">
              <div v-if="v.enterpriseState !== 5"
                   style="display: flex;align-items: center;justify-content: flex-end;position: absolute;top: 16px;right: 16px;cursor: pointer"
                   @click="openDeleteDialog(v.title,v.id)">
                x
              </div>
              <div class="info">
                <div class="info-l">
                  <img v-if="v.enterpriseState === 5"
                       alt=""
                       src="">
                  <img v-else
                       alt=""
                       src="">
                </div>
                <div class="info-r">
                  <div class="infotitle">
                    {{ v.enterpriseName }}
                  </div>
                  <div class="type flexleft">
                    <div class="type-level" v-if="v.enterpriseState !== 5">
                      <div>
                        <i class="el-icon-s-custom" style="color: #b1b1b1"></i>
                        <span style="font-size: 12px">未认证</span>
                      </div>
                    </div>

                    <div v-else class="type-admin flexbetween">
                      <i class="el-icon-s-custom" style="color:#1063f1;"></i>
                      <p>管理员{{ v.adminName }}</p>
                    </div>
                  </div>
                </div>
              </div>
              <div class="info-list">
                <div v-if="v.enterpriseState === 5" class="list-item">
                  <div class="list-item-left">
                    剩余合同数<span>{{ v.signNum }}</span> 份
                  </div>
                  <div class="list-item-right">
                    <el-button type="text" @click="goBuy">购买 ></el-button>
                  </div>
                </div>
                <div v-else class="list-item">
                  <div class="list-item-left">
                    认证后可体验更多功能
                  </div>
                </div>
              </div>
              <div class="btm flexcenter">
                <el-link v-if="v.enterpriseState === 5 && v.isAdmin===1 " :underline="false" type="primary"
                         @click="goBusiness(v.id, v.enterpriseName)">进入企业控制台
                </el-link>
                <el-link v-if="v.enterpriseState !=5 " :underline="false" type="primary" @click="goCertification(v)">去企业认证</el-link>
              </div>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="problem">
              <div class="column-title">
                如何升级为企业用户
              </div>
              <div class="" style="margin-top: 10px">
                <base-question/>
              </div>
            </div>

          </el-col>
        </el-row>
      </div>
    </div>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :visible.sync="dialogDeleteVisible"
               destroy-on-close
               title="删除企业" width="20%" @close="dialogDeleteCancelFunc">
      <p style="font-size: 16px;display: flex;align-items: center"><i class="el-icon-warning"
                                                                      style="color: #ffa811;font-size: 22px"></i>确定要删除{{ deleteTitle
        }}吗
      </p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDeleteCancelFunc">取 消</el-button>
        <el-button :loading="deleteLoading" type="primary" @click="dialogDelete">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :visible.sync="dialogFormVisible"
               destroy-on-close
               title="创建企业" width="40%" @close="dialogFormVisibleFunc">
      <el-row style="margin-bottom: 40px">
        <el-col :span="24">
          <el-alert
            :closable="false"
            show-icon
            title="创建企业并实名成功后，将自动成为该企业管理员，并生成对应企业名称的公章"
            type="info"
          >
          </el-alert>
        </el-col>
      </el-row>
      <el-row class="flexcenter">
        <el-col :span="22">
          <p style="margin-bottom: 30px">请输入您要创建的企业名称，并确保与营业执照上的名称一致</p>
          <el-autocomplete
            v-model="businessState"
            :fetch-suggestions="querySearchAsync"
            :maxlength="100"
            :trigger-on-focus="false"
            highlight-first-item
            placeholder="请输入内容"
            show-word-limit
            style="width: 100%;height: 60px"
            type="textarea"
            @input="businessInput"
            @select="handleSelect"
            @keydown.native.enter="dialogSubmit"
          >
            <template slot-scope="{ item }">
              <div class="flexbetween" style="padding: 4px 0">
                <span v-html="item.serchName"></span>
                <span style="background-color: #ededed;color: #666666;padding: 0 6px">工商注册名称</span>
              </div>
            </template>
          </el-autocomplete>
          <span v-show="isCertification" style="color: #e61717">该企业已经认证过，请联系管理员邀请您加入，或者点击 <el-button
            type="text" @click="addCompany" style="color: #1D6BF1">申请加入该企业</el-button> [需管理员同意]</span>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleFunc">取 消</el-button>
        <el-button :disabled="businessState === ''" :loading="dialogSubmitLoading" type="primary" @click="dialogSubmit">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import {
  getAllCompany,
  getLikeEnterpriseInformation,
  getEnterpriseInformationAuthentication,
  postAdd, postDeleteUserEnterprise
} from "@/api/company";
import BaseQuestion from "@/components/BaseQuestion.vue";

export default {
  name: "allBusiness",
  components: { BaseQuestion },
  data() {
    return {
      input: "",
      dialogFormVisible: false,
      allCompany: [],
      businessState: "",
      timeout: null,
      dialogSubmitLoading: false,
      isCertification: false,
      companyInfo: {},
      dialogDeleteVisible: false,
      deleteTitle: "",
      deleteId: null,
      deleteLoading: false,
      enterpriseId: null
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      //   加载全部企业
      getAllCompany({ userId: Cookies.get("admin_id") }).then(res => {
        if (res.code === 200) {
          console.log("加载全部企业");
          this.allCompany = res.result;
        }
      });
    },
    //   搜索企业
    searchCompany() {
      getAllCompany({ userId: Cookies.get("admin_id"), enterpriseName: this.input }).then(res => {
        if (res.code === 200) {
          this.allCompany = res.result;
        }
      });
    },
    goBusiness(id, name) {
      this.$router.push({ name: "business" },()=>{});
      localStorage.setItem("companyid", id);
      localStorage.setItem("bussinesname", name);
    },
    shutup() {
      this.$router.push({ name: "work" },()=>{});
    },
    // 模糊搜索全国企业
    querySearchAsync(queryString, cb) {
      if (queryString) {
        getLikeEnterpriseInformation({ keyword: queryString }).then(res => {
          console.log("模糊搜索全国企业");
          res.data.forEach(item => {
            item.serchName = item.companyName.replace(new RegExp(this.businessState, "ig"), val => `<span style="color: #ff0000">${val}</span>`);
          });
          cb(res.data);
        });
      }
    },
    handleSelect(item) {
      this.businessState = item.companyName;
      this.companyInfo = item;
      console.log("记录企业三要素");
      localStorage.setItem("servicekeywords", JSON.stringify(this.companyInfo));
    },
    dialogFormVisibleFunc() {
      this.dialogFormVisible = false;
      this.businessState = "";
    },
    dialogSubmit() {
      this.dialogSubmitLoading = true;
      // 判断企业是否认证过
      getEnterpriseInformationAuthentication({
        enterpriseName: this.businessState,
        userId: Cookies.get("admin_id")
      }).then(res => {
        console.log("判断企业是否认证过");
        this.dialogSubmitLoading = false;
        if (res.result.state === 0 || res.result.state === 1) {
          this.dialogFormVisible = false;
          this.$router.push({
            name: "authentication"
          },()=>{});
        } else if (res.result.state === 2) {
          this.isCertification = true;
          this.enterpriseId = res.result.enterpriseId;
        }
      });
    },
    addCompany() {
      // 已认证申请加入该企业
      postAdd({ enterpriseId: this.enterpriseId, userId: Cookies.get("admin_id") }).then(res => {
        if (res.code === 200) {
          this.$message.success("申请提交成功");
        }
      });
    },
    businessInput() {
      this.isCertification = false;
    },
    openDeleteDialog(title, id) {
      this.dialogDeleteVisible = true;
      this.deleteTitle = title;
      this.deleteId = id;
    },
    dialogDeleteCancelFunc() {
      this.dialogDeleteVisible = false;
    },
    dialogDelete() {
      this.deleteLoading = true;
      console.log("删除企业");
      postDeleteUserEnterprise({ enterpriseId: this.deleteId, userId: Cookies.get("admin_id") }).then(res => {
        this.deleteLoading = false;
        if (res.code === 200) {
          this.dialogDeleteVisible = false;
          this.$message.success("删除成功");
          this.init();
        }
      });
    },
    goCertification(info) {
      const { legalPerson, creditCode, enterpriseName } = info;
      localStorage.setItem("servicekeywords", JSON.stringify({
        faRen: legalPerson,
        creditCode,
        companyName: enterpriseName
      }));
      this.$router.push({
        name: "authentication"
      },()=>{});
    },
    goBuy(){
      this.$router.push({
        path: '/business/relevance'
      },()=>{}).then()
    }
  }

};
</script>

<style lang="scss">
.allBusiness {
  .el-submenu .el-menu-item {
    min-width: 0;
  }
}
</style>

<style lang="scss" scoped>
.allBusiness {
  // background-color: #fff;

  .header {
    background-color: #262729;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    width: 100%;
    box-sizing: border-box;
    height: 60px;
  }

  .logoimg {
    width: 110px;
    height: 28px;
    margin-top: 5px;
  }

  .shutup {
    background-color: #3b4049;
    padding: 15px 20px;
  }

  .title {
    color: #fff;
    padding: 0 30px;
    border-left: 1px solid #fff;
    font-size: 20px;
  }

  .content {
    width: 100vw;
    // height: calc( 100vh - 60px );
    margin-top: 60px;
    box-sizing: border-box;

    .companyList {
      width: 434px;
      height: 290px;
      background-color: #fff;
      margin-bottom: 8px;
      position: relative;

      &:nth-child(n) {
        margin-right: 8px;
      }

      .info {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        padding: 30px 24px 0;
        overflow: hidden;
        margin-bottom: 18px;

        .info-l {
          width: 48px;
          height: 48px;
          margin-right: 12px;
          border: 1px solid #ececec;
          border-radius: 50%;
          overflow: hidden;

          img {
            display: block;
            width: 100%;
            height: 100%;
          }
        }

        .info-r {
          flex: 1;
          justify-content: center;

          .infotitle {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            font-size: 16px;
            line-height: 22px;
            margin-bottom: 8px;
            font-weight: 500;
            color: #000;
          }

          .type-level {
            img {
              display: block;
              height: 18px;
              margin-right: 8px;
              width: 60px;
            }
          }

          .type-admin {
            background: #f5f5f5;
            border-radius: 2px;
            height: 20px;
            display: -webkit-box;
            display: -ms-flexbox;
            display: flex;
            -webkit-box-align: center;
            -ms-flex-align: center;
            align-items: center;
            font-size: 12px;
            padding: 0 5px;
          }
        }
      }

      .info-list {
        height: 150px;
        padding: 0 24px;

        .list-item {
          height: 40px;
          background: #f7f8fa;
          padding: 0 16px;
          line-height: 40px;
          margin-bottom: 8px;
          display: -webkit-box;
          display: -ms-flexbox;
          display: flex;

          .list-item-left {
            font-size: 14px;
            -webkit-box-flex: 1;
            -ms-flex: 1;
            flex: 1;
            margin-right: 12px;
          }

          .list-item-right {
            font-size: 12px;
          }
        }
      }

      .btm {
        height: 44px;
        border-top: 1px solid #f2f2f2;
      }

      .delete {
        position: absolute;
        right: 0;
        top: 0;
      }
    }

    .problem {
      background-color: #fff;
      padding: 24px;

      .column-title {
        padding-left: 16px;
        position: relative;
        font-size: 16px;
        line-height: 22px;
        font-weight: 500;
        // margin-bottom: 24px;
        &:before {
          content: "";
          position: absolute;
          width: 4px;
          height: 16px;
          top: 3px;
          left: 0;
          background: #1063f1;
        }
      }
    }
  }
}
</style>